package com.hualan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hualan.mapper.ArticleCategoryMapper;
import com.hualan.pojo.ArticleCategory;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Result;
import com.hualan.service.ArticleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    @Override
    public PageResult queryAllArticleCategory(String categoryName,Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleCategory> articleCategories = articleCategoryMapper.queryAllArticleCategory(categoryName);
        Page<ArticleCategory> p=(Page<ArticleCategory>)articleCategories;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Result addArticleCategory(ArticleCategory articleCategory) {
        try{
            articleCategoryMapper.addArticleCategory(articleCategory);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加文章分类失败");
        }

    }

    @Override
    public Result updateArticleCategory(ArticleCategory articleCategory) {
        try{
            articleCategoryMapper.updateArticleCategory(articleCategory);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新文章分类失败");
        }
    }

    @Override
    public Result deleteArticleCategory(List<Integer> ids) {
        try {
            articleCategoryMapper.deleteArticleCategory(ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除文章分类失败");
        }
    }
}
